package com.jeasonzhao.report.engine.repo;

import java.util.Collection;
import java.util.Hashtable;

import com.jeasonzhao.report.engine.Configuration;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.msg.MessageCodes;

public class ReportRepository
{
    private static volatile ReportRepository m_instance = null;
    private Hashtable<String,Report> m_hashReports = new Hashtable<String,Report>();
    private ReportRepository()
    {
    }

    public Collection<Report> getAllReports()
    {
        return this.m_hashReports.values();
    }

    public static ReportRepository getInstance()
    {
        if(null == m_instance)
        {
            synchronized(ReportRepository.class)
            {
                m_instance = new ReportRepository();
            }
        }
        return m_instance;
    }

    public Report get(String strReportID)
    {
        Configuration.getInstance().refreshConfigurations();
        if(m_hashReports.containsKey(strReportID))
        {
            return new Report(m_hashReports.get(strReportID));
        }
        else
        {
            return null;
        }
    }

    public void add(Report rpt)
    {
        if(null != rpt)
        {
            Configuration.getInstance().getLogger().info(MessageCodes.format(MessageCodes.CFG_ADDREPORT,MessageCodes.getLongDescription(rpt)));
            m_hashReports.put(rpt.getReportId(),rpt);
        }
    }
}
